package com.edais.actions;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.edais.service.FinancePlanService;
import com.edais.util.CommonUtil;
import com.edais.util.Consts;
import com.edais.util.Message;
import com.edais.vo.FinanceListVo;
import com.edais.vo.FinanceVo;
import com.edais.vo.PlanCategorysVo;

@Controller
@RequestMapping("/finance")

public class FinancePlanAction  implements Serializable{
	private static final Log logger = LogFactory.getLog(FinancePlanAction.class);

	@Autowired
	private FinancePlanService financeService;

	
	/**
	 * E400031 获取产品列表
	 * 
	 * @param request
	 * @return response
	 * @throws Exception
	 */
	@RequestMapping(value = "/getFinanceList")
	public void getFinanceList(HttpServletRequest request, HttpServletResponse response){
		Integer current = 0;
		Integer pageSize = 0;
		
		// 解密请求参数
		Map<String, String> paramsMap = CommonUtil.decryptParamters(request);
		String currentStr = paramsMap.get(Consts.Page.PAGE_CURRENT_NAME);   //当前页数
		String pageSizeStr = paramsMap.get(Consts.Page.PAGE_SIZE_NAME);     //页大小

		try{
			if(currentStr != null && !("".equals(currentStr))){
				current = Integer.parseInt(currentStr);
			}
			if(pageSizeStr != null && !("".equals(pageSizeStr))){
				pageSize = Integer.parseInt(pageSizeStr);
			}
			
			// 获取所有的产品条数
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("selCondition1", Consts.FinanceBuyChannel.BUYCHANNELFORMOBILE);
			map.put("selCondition2", Consts.FinanceBuyChannel.BUYCHANNELFORWANDM);
			String totalNum = financeService.getFinancesCount(map);
			// 抽取产品
			//组装起始条数
			map.put("rowBegin",(current-1)*pageSize+1);
			map.put("rowEnd",current*pageSize);
			List<FinanceListVo> financeList = financeService.getFinanceList(map);
			
			Map<String, Object> mapResult = new HashMap<String, Object>();
			mapResult.put("totalNum", totalNum);
			mapResult.put("listSize", String.valueOf(financeList.size()));
			mapResult.put("list", financeList);
			
			mapResult.put("resmsg_cn", "");
			String result = CommonUtil.setResultInfo(mapResult, Consts.SUCCESS_CODE, Consts.SUCCESS_DESCRIBE);
			// 处理返回结果
			CommonUtil.responseJson(result, response);
			
		}catch(Exception e){
			logger.debug("/finance/getFinanceList异常-----" + e.getMessage());
			
			String result = CommonUtil.setResultStringCn(new HashMap<String, Object>(),
								Consts.ERROR_CODE, Consts.ErrorMsg.MSG00001,
								Message.CnDescriptionExpression.BUSINESSE_ERROR);
			// 处理返回结果
			CommonUtil.responseJson(result, response);
		}
		return;
	}
	
	/**
	 * 获取产品一级分类列表
	 */
	@RequestMapping(value = "/getPlanCategory")
	public void getPlanCategory(HttpServletRequest request, HttpServletResponse response){
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try{
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("selCondition1", Consts.FinanceBuyChannel.BUYCHANNELFORMOBILE);
			map.put("selCondition2", Consts.FinanceBuyChannel.BUYCHANNELFORWANDM);
			map.put("enableBuy", "Y");
			
			//jsonMap = financeService.getCategoryList(map);
			List<PlanCategorysVo> planCategorysVo = financeService.getCategorys(map);
			jsonMap.put("planCategoryList", planCategorysVo);
			
			String result = CommonUtil.setResultStringCn(
				jsonMap,Consts.SUCCESS_CODE, Consts.SUCCESS_DESCRIBE, "");
			// 处理返回结果
			CommonUtil.responseJson(result, response);
		}catch(Exception e){
			logger.debug("finance/getPlanCategory 异常" + e.getMessage());
			String result = CommonUtil.setResultStringCn(
				new HashMap<String, Object>(),Consts.ERROR_CODE, Consts.ErrorMsg.MSG00001,Message.CnDescriptionExpression.BUSINESSE_ERROR);
			// 处理返回结果
			CommonUtil.responseJson(result, response);
		}
	}
	
	/** 
	 * 根据一级分类获取所属产品
	 */
	@RequestMapping(value = "/getPlansByKind")
	public void getPlansByKind(HttpServletRequest request, HttpServletResponse response){
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		// 解密请求参数
		Map<String, String> paramsMap = CommonUtil.decryptParamters(request);
		String kindId = paramsMap.get("categoryId");     

		if (StringUtils.isBlank(kindId)) { 
			logger.debug("finance/getPlansByKind 传入参数为空");
			String result = CommonUtil.setResultStringCn(
				new HashMap<String, Object>(),Consts.ERROR_CODE, Consts.ErrorMsg.MSG00001,Message.CnDescriptionExpression.BUSINESSE_ERROR);
			// 处理返回结果
			CommonUtil.responseJson(result, response);
			return;
		}

		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("kindId", kindId);
			map.put("selCondition1", Consts.FinanceBuyChannel.BUYCHANNELFORMOBILE);
			map.put("selCondition2", Consts.FinanceBuyChannel.BUYCHANNELFORWANDM);
			map.put("enableBuy", "Y");
			List<FinanceVo> planList = financeService.getPlansByKind(map);
//
			jsonMap.put("planList", planList);
			//jsonMap = financeService.getPlanListByKind(map);
			String result = CommonUtil.setResultStringCn(
				jsonMap,Consts.SUCCESS_CODE, Consts.SUCCESS_DESCRIBE, "");
			// 处理返回结果
			CommonUtil.responseJson(result, response);

		} catch (Exception e) {
			logger.error("/investmentCenter/getFinanceDetail 异常:" + e.getMessage());

			String result = CommonUtil.setResultStringCn(new HashMap<String, Object>(), Consts.ERROR_CODE, 
				Consts.ErrorMsg.MSG00001,Message.CnDescriptionExpression.BUSINESSE_ERROR);
			// 处理返回结果
			CommonUtil.responseJson(result, response);
	
		}
	}
}
